import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author LKQ
 * @date 2021/12/28 21:20
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {1,-2147483648,-2147483648,2};
        System.out.println(solution.thirdMax(nums));
    }

    public int thirdMax(int[] nums) {
        int n = nums.length;
        Arrays.sort(nums);
        if (n < 3) {
            return nums[n-1];
        }
        int thirdMax = 0;
        Set<Integer> set = new HashSet<>();
        for (int i = n-1; i >= 0; i--) {
            if (set.contains(nums[i])) {
                continue;
            }
            set.add(nums[i]);
            if (set.size() == 3) {
                thirdMax = nums[i];
                break;
            }
        }
        return set.size() < 3 ? nums[n-1] : thirdMax;
    }
}
